<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="景点名称"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="景点描述"></el-input>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="详细地址"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactName">
        <el-input v-model="dataForm.contactName" placeholder="联系人姓名"></el-input>
      </el-form-item>
      <el-form-item label="热度" prop="hot">
        <el-input v-model="dataForm.hot" placeholder="热度"></el-input>
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input v-model="dataForm.province" placeholder="省份"></el-input>
      </el-form-item>
      <el-form-item label="市" prop="municipal">
        <el-input v-model="dataForm.municipal" placeholder="市"></el-input>
      </el-form-item>
      <el-form-item label="区" prop="district">
        <el-input v-model="dataForm.district" placeholder="区"></el-input>
      </el-form-item>
      <el-form-item label="添加时间" prop="addTime">
        <el-input v-model="dataForm.addTime" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="所有图片" prop="picId">
        <el-input v-model="dataForm.picId" placeholder="多个id用;隔开"></el-input>
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input v-model="dataForm.longitude" placeholder="经度(最多保留小数点7位)"></el-input>
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input v-model="dataForm.latitude" placeholder="纬度(最多保留小数点7位)"></el-input>
      </el-form-item>
      <el-form-item label="周边吃" prop="surroundingEat">
        <el-input v-model="dataForm.surroundingEat" placeholder="周边吃"></el-input>
      </el-form-item>
      <el-form-item label="周边住" prop="surroundingLive">
        <el-input v-model="dataForm.surroundingLive" placeholder="周边住"></el-input>
      </el-form-item>
      <el-form-item label="周边玩" prop="surroundingPlay">
        <el-input v-model="dataForm.surroundingPlay" placeholder="周边玩"></el-input>
      </el-form-item>
      <el-form-item label="所属分类" prop="tagIds">
        <el-checkbox-group @change="change" v-model="dataForm.tagIds">
          <el-checkbox v-for="tag in tags" :label="tag.id" :key="tag.id">{{tag.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        tags: [],
        dataForm: {
          id: 0,
          name: '',
          description: '',
          address: '',
          phone: '',
          contactName: '',
          hot: '',
          province: '',
          municipal: '',
          district: '',
          addTime: '',
          picId: '',
          longitude: '',
          latitude: '',
          tagIds: [],
          surroundingEat: '',
          surroundingLive: '',
          surroundingPlay: ''
        },
        dataRule: {
          name: [
            {required: true, message: '景点名称不能为空', trigger: 'blur'}
          ],
          address: [
            {required: true, message: '详细地址不能为空', trigger: 'blur'}
          ],
          phone: [
            {required: true, message: '联系电话不能为空', trigger: 'blur'}
          ],
          contactName: [
            {required: true, message: '联系人姓名不能为空', trigger: 'blur'}
          ],
          hot: [
            {required: true, message: '热度不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    created () {
      this.getAllTags()
    },
    methods: {
      change (e) {
        console.log(e)
      },
      getAllTags () {
        this.$http({
          url: this.$http.adornUrl(`/haiyou/tag/list?page=1&limit=100`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.tags = data.page.list
            console.log(this.tags)
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/haiyou/scene/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.scene.name
                this.dataForm.description = data.scene.description
                this.dataForm.address = data.scene.address
                this.dataForm.phone = data.scene.phone
                this.dataForm.contactName = data.scene.contactName
                this.dataForm.hot = data.scene.hot
                this.dataForm.province = data.scene.province
                this.dataForm.municipal = data.scene.municipal
                this.dataForm.district = data.scene.district
                this.dataForm.addTime = data.scene.addTime
                this.dataForm.picId = data.scene.picId
                this.dataForm.tagIds = data.scene.tagIds
                this.dataForm.longitude = data.scene.longitude
                this.dataForm.latitude = data.scene.latitude
                this.dataForm.surroundingEat = data.scene.surroundingEat
                this.dataForm.surroundingLive = data.scene.surroundingLive
                this.dataForm.surroundingPlay = data.scene.surroundingPlay
                console.log(this.dataForm)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/haiyou/scene/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'description': this.dataForm.description,
                'address': this.dataForm.address,
                'phone': this.dataForm.phone,
                'contactName': this.dataForm.contactName,
                'hot': this.dataForm.hot,
                'province': this.dataForm.province,
                'municipal': this.dataForm.municipal,
                'district': this.dataForm.district,
                'addTime': this.dataForm.addTime,
                'picId': this.dataForm.picId,
                'tagIds': this.dataForm.tagIds,
                'latitude': this.dataForm.latitude,
                'longitude': this.dataForm.longitude,
                'surroundingEat': this.dataForm.surroundingEat,
                'surroundingLive': this.dataForm.surroundingLive,
                'surroundingPlay': this.dataForm.surroundingPlay
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
